﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace 计时器
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private TimeSpan _Time;
        public TimeSpan Time
        {
            get => _Time; 
            set
            {
                _Time = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Time"));
            }
        }

        private DateTime StartTime;
        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;

            StartTime = DateTime.Now;

            Thread Timer = new Thread(new ThreadStart(TimerTick));
            Timer.Start();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        void TimerTick()
        {
            while (true)
            {
                Dispatcher.Invoke(new Action(() =>
                {
                    Time = DateTime.Now - StartTime;
                }));
            }
        }
    }

}
